<template>
  <!-- 整体容器 ------------------------->
  <el-container>
    <!-- 头部区 ------------------>
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>黑马电商后台管理系统</span>
      </div>
      <el-button type="info" round @click="Logout">退出登录</el-button>
    </el-header>

    <!-- 主体区 ------------------->
    <el-container>
      <!-- 左侧边栏 ----------------------->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠菜单按钮 -->
        <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
        <!-- 1.菜单组件 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          router
          :collapse-transition="false"
          :collapse="isCollapse"
          :default-active="clickmenu"
        >
          <!-- 2.子菜单：容器 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 子菜单 文字模板 -->
            <template slot="title">
              <i :class="item.iconclass"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 3.菜单项：可以用来点击 导航的-->
            <el-menu-item
              :index="'/' + sonitem.path"
              v-for="sonitem in item.children"
              :key="sonitem.id"
              @click="saveMenu('/' + sonitem.path)"
            >
              <!-- 菜单项 文字模板 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ sonitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'app',
  data: function () {
    return {
      // 菜单数组(通过异步请求服务器接口获得)
      menuList: [],
      isCollapse: false,
      clickmenu: ''
    }
  },
  methods: {
    // 退出函数
    Logout() {
      this.$msgbox
        .confirm('你将要退出登录哦', '请确认', {
          confirmButtonText: '确定退出',
          cancelButtonText: '取消操作',
          type: 'info'
        })
        .then(() => {
          window.sessionStorage.removeItem('token')
          this.$router.push('/login')
          this.$message({
            type: 'success',
            message: '退出登录成功!'
          })
        })
        .catch()
    }
  },
  // 钩子函数
  async created() {
    // 1.异步请求菜单列表
    // 异步请求 左侧菜单权限
    const { data: resBody } = await this.$axios.get('menus')
    // 如果 失败了，显示错误消息，并退出函数运行
    if (resBody.meta.status !== 200) return this.$msg.error(resBody.meta.msg)
    // 如果成功，将 返回的 数组 保存到 data中，供组件视图使用
    this.menuList = resBody.data
  },
  saveMenu(val) {
    this.clickmenu = val
    window.sessionStorage.setItem('c')
  }
}
</script>

<style scoped lang="less">
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont {
  margin-right: 10px;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
